package ATM.frame;

import ATM.service.UserService;
import ATM.service.impl.UserServiceImpl;
import ATM.util.DBUtil;
import ATM.pojo.UserDO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class LoginPanel extends JPanel implements ActionListener {
    private UserService userService = new UserServiceImpl();

    //计数块
    private int count = 3;
    //组件块
    private JButton jButton;
    private JTextField jTextField;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JPasswordField jPasswordField;
    //背景块
    private Icon buttonIcon = util.Util.getIcon("/images/buttonLand.jpg");

    //属性块
    private ATMmain atMmain;

    private ArrayList<UserDO> userList;

    public LoginPanel(ATMmain atMmain) {
        this.atMmain = atMmain;

        this.setLayout(null);

        this.setOpaque(false);

        jLabel1 = new JLabel("账户");
        jLabel1.setBounds(400, 280, 50, 50);
        jLabel1.setForeground(Color.blue);
        jLabel1.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(jLabel1);
        jTextField = new JTextField("");
        jTextField.setBounds(500, 285, 200, 30);
        this.add(jTextField);

        jLabel2 = new JLabel("密码");
        jLabel2.setBounds(400, 385, 50, 50);
        jLabel2.setFont(new Font("宋体", Font.BOLD, 20));
        jLabel2.setForeground(Color.blue);
        this.add(jLabel2);
        jPasswordField = new JPasswordField("");
        jPasswordField.setBounds(500, 390, 200, 30);
        this.add(jPasswordField);

        jButton = new JButton(buttonIcon);
        jButton.setBounds(450, 460, 140, 50);
        this.add(jButton);

        jButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton) {
              /*  for (int i = 0; i < atMmain.getUserList().size(); i++) {
                    if (jTextField.getText().equals(atMmain.getUserList().get(i).getUserName()) && jPasswordField.getText().equals(atMmain.getUserList().get(i).getUserPassWord())) {
                        JOptionPane.showMessageDialog(atMmain, "登陆成功");
                        atMmain.setLoginUser(atMmain.getUserList().get(i));
                        count=3;
                        atMmain.switchPanel("menuPanel");
                        return;
                    }
                }*/
            String username = jTextField.getText();
            String pwd = String.valueOf(jPasswordField.getPassword());

            UserDO user=userService.getByUsernameAndPwd(username,pwd);

            if (user !=null){
                atMmain.setLoginUser(user);
                atMmain.switchPanel("menuPanel");
                return;
            }

            count--;
            if (count==0){
                JOptionPane.showMessageDialog(atMmain,"次数用尽，卡被吞掉");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(atMmain, "账户或密码错误，请重试。你还有"+count+"次机会");
        }
    }
}
